﻿/*---------------------------------------------------------------------------------
-----------------***DaLianQianYi***-----------------------
-----------------作者：大连仟亿科技------------------------------------------------------
-----------------联系作者：www.qianyikeji.com----------------------------------
-----------------时间：2017/10/24 9:19:20------------------------------------------
----------------------------------------------------------------------------------*/


using Donal.Xms.Common;
using Newtonsoft.Json;
using System;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Donal.Xms.Web.qianyi.common
{
    public partial class action_list : Web.UI.ManagePage
    {
        protected int totalCount;
        protected int page;
        protected int pageSize;

        protected int action = 0;
        protected string status = string.Empty;
        protected string conditions = string.Empty;
        protected string keywords = string.Empty;
        protected string expiration_form = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.pageSize = GetPageSize(10); //每页数量
            this.keywords = DTRequest.GetQueryString("keywords");
            action = DTRequest.GetQueryInt("action");
            status = DTRequest.GetQueryString("status");
            conditions = DTRequest.GetQueryString("conditions");
            expiration_form = DTRequest.GetQueryString("expiration_form");


            if (!Page.IsPostBack)
            {
                ChkAdminLevel("actions", DTEnums.ActionEnum.View.ToString()); //检查权限
                RptBind("id>0" + CombSqlTxt(keywords), "sort_id, id desc");
            }
        }

        #region 数据绑定=================================
        private void RptBind(string _strWhere, string _orderby)
        {
            this.page = DTRequest.GetQueryInt("page", 1);
            txtKeywords.Text = this.keywords;

            ddlStatus.SelectedValue = status;
            ddlConditions.SelectedValue = conditions;
            ddlExpiration_form.SelectedValue = expiration_form;
            ddlActionType.SelectedValue = action.ToString();

            BLL.actions bll = new BLL.actions();
            this.rptList.DataSource = bll.GetList(this.pageSize, this.page, _strWhere, _orderby, out this.totalCount);
            this.rptList.DataBind();

            //绑定页码
            txtPageNum.Text = this.pageSize.ToString();
            string pageUrl = Utils.CombUrlTxt("action_list.aspx", "status={0}&conditions={1}&expiration_form={2}&keywords={3}&action={4}&page={5}", status, conditions, expiration_form, keywords, action.ToString(), "__id__");
            PageContent.InnerHtml = Utils.OutPageList(this.pageSize, this.page, this.totalCount, pageUrl, 8);
        }
        #endregion

        #region 组合SQL查询语句==========================
        protected string CombSqlTxt(string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();
            _keywords = _keywords.Replace("'", "");

            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append(" and (title like  '%" + _keywords + "%' or contents like '%" + _keywords + "%')");
            }

            if (status != "")
            {
                switch (status)
                {
                    case "z":
                        strTemp.Append(" and status=1");
                        break;
                    case "j":
                        strTemp.Append(" and status=0");
                        break;
                }
            }

            if (conditions != "")
            {
                switch (conditions)
                {
                    case "w":
                        strTemp.Append(" and (conditions_amount=0 or conditions_amount is null)");
                        break;
                    case "y":
                        strTemp.Append(" and conditions_amount > 0");
                        break;
                }
            }

            if (expiration_form != "")
            {
                switch (expiration_form)
                {
                    case "zd":
                        strTemp.Append(" and expiration_form=0");
                        break;
                    case "yb":
                        strTemp.Append(" and expiration_form=1");
                        break;
                }
            }

            if (action > 0)
            {
                strTemp.Append(" and category=" + (action - 1));
            }

            return strTemp.ToString();
        }
        #endregion

        #region 返回每页数量=============================
        private int GetPageSize(int _default_size)
        {
            int _pagesize;
            if (int.TryParse(Utils.GetCookie("coupons_page_size", "Donal.XmsPage"), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    return _pagesize;
                }
            }
            return _default_size;
        }
        #endregion

        //关健字查询
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Redirect();
        }

        //设置分页数量
        protected void txtPageNum_TextChanged(object sender, EventArgs e)
        {
            int _pagesize;
            if (int.TryParse(txtPageNum.Text.Trim(), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    Utils.WriteCookie("coupons_page_size", "Donal.XmsPage", _pagesize.ToString(), 14400);
                }
            }
            Redirect();
        }

        #region 下拉选择条件
        /// <summary>
        /// 状态过滤
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            Redirect();
        }
        /// <summary>
        /// 门槛过滤
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlConditions_SelectedIndexChanged(object sender, EventArgs e)
        {
            Redirect();
        }

        /// <summary>
        /// 过期形式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlExpiration_form_SelectedIndexChanged(object sender, EventArgs e)
        {
            Redirect();
        }

        private void Redirect()
        {
            Response.Redirect(Utils.CombUrlTxt("action_list.aspx", "status={0}&conditions={1}&expiration_form={2}&keywords={3}&action={4}", ddlStatus.SelectedValue, ddlConditions.SelectedValue, ddlExpiration_form.SelectedValue, txtKeywords.Text, ddlActionType.SelectedValue));
        }
        #endregion

        #region 删除        

        /// <summary>
        /// 禁用优惠券
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnLocked_Click(object sender, EventArgs e)
        {
            ChkAdminLevel("actions", DTEnums.ActionEnum.Audit.ToString()); //检查权限
            int sucCount = 0;
            int errorCount = 0;
            BLL.actions bll = new BLL.actions();
            
            for (int i = 0; i < rptList.Items.Count; i++)
            {              
                int id = Convert.ToInt32(((HiddenField)rptList.Items[i].FindControl("hidId")).Value);
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("chkId");
                if (cb.Checked)
                {                    
                    if (bll.Delete(id))
                    {                       
                        sucCount += 1;
                    }
                    else
                    {
                        errorCount += 1;
                    }
                }
            }

            JscriptMsg("成功删除活动" + sucCount + "条，失败" + errorCount + "条！", Utils.CombUrlTxt("action_list.aspx", "status={0}&conditions={1}&expiration_form={2}&keywords={3}&action={4}", ddlStatus.SelectedValue, ddlConditions.SelectedValue, ddlExpiration_form.SelectedValue, txtKeywords.Text, ddlActionType.SelectedValue));

        }
        #endregion

        //保存排序
        protected void btnSave_Click(object sender, EventArgs e)
        {
            ChkAdminLevel("actions", DTEnums.ActionEnum.Edit.ToString()); //检查权限
            BLL.actions bll = new BLL.actions();

            for (int i = 0; i < rptList.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)rptList.Items[i].FindControl("hidId")).Value);
                int sortId;
                if (!int.TryParse(((TextBox)rptList.Items[i].FindControl("txtSortId")).Text.Trim(), out sortId))
                {
                    sortId = 99;
                }
                bll.UpdateField(id, "sort_id=" + sortId.ToString());
            }
            AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), "保存优惠活动排序"); //记录日志
            JscriptMsg("保存排序成功！", Utils.CombUrlTxt("action_list.aspx", "status={0}&conditions={1}&expiration_form={2}&keywords={3}&action={4}", ddlStatus.SelectedValue, ddlConditions.SelectedValue, ddlExpiration_form.SelectedValue, txtKeywords.Text, ddlActionType.SelectedValue));
        }

        protected void ddlActionType_SelectedIndexChanged(object sender, EventArgs e)
        {
            Redirect();
        }
    }
}

